package com.amway.qy.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amway.qy.core.constants.QyFetchChildFlag;
import com.amway.qy.core.constants.QyMemberStatus;
import com.amway.qy.core.constants.QyWechatUrls;
import com.amway.qy.core.service.QyAccessTokenService;
import com.amway.qy.core.service.QyMemberService;
import com.amway.qy.resource.member.QyMember;
import com.amway.qy.resource.member.QyMemberBatchDeleteReq;
import com.amway.qy.resource.result.BaseResult;
import com.amway.qy.resource.result.member.QyMemberListResult;
import com.amway.qy.resource.result.member.QyMemberResult;
import com.amway.qy.util.common.QyHttpClientUtils;
import com.google.gson.Gson;

public class QyMemberServiceImpl implements QyMemberService {
	
	private static final Logger logger = LoggerFactory.getLogger(QyMemberServiceImpl.class);
		
	private QyAccessTokenService accessTokenManager;
	
	public QyMemberServiceImpl(QyAccessTokenService accessTokenManager){
		this.accessTokenManager = accessTokenManager;
	}

	@Override
	public BaseResult createMember(QyMember member) {
		 
		return new Gson().fromJson(createMemberInRaw(member), BaseResult.class);
	}

	@Override
	public String createMemberInRaw(QyMember member) {
		logger.debug("createMemeberInRaw--> Member:"+member);
		
		List<NameValuePair> paramsMap = new ArrayList<NameValuePair>();
		paramsMap.add(new BasicNameValuePair("access_token", accessTokenManager.getAccessToken()));
	
		return QyHttpClientUtils.httpsPostJson(QyWechatUrls.CREATE_MEMBER_URL, paramsMap,new Gson().toJson(member));
	}

	@Override
	public BaseResult updateMember(QyMember member) {
		return new Gson().fromJson(updateMemberInRaw(member), BaseResult.class);
	}

	@Override
	public String updateMemberInRaw(QyMember member) {
		logger.debug("updateMemeberInRaw--> Member:"+member);
		
		List<NameValuePair> paramsMap = new ArrayList<NameValuePair>();
		paramsMap.add(new BasicNameValuePair("access_token", accessTokenManager.getAccessToken()));
	
		return QyHttpClientUtils.httpsPostJson(QyWechatUrls.UPDATE_MEMBER_URL, paramsMap,new Gson().toJson(member));
	}

	@Override
	public BaseResult deleteMember(String userId) {
		return new Gson().fromJson(deleteMemberInRaw(userId), BaseResult.class);
	}

	@Override
	public String deleteMemberInRaw(String userId) {
		logger.debug("deleteMemeberInRaw--> User Id:"+userId);
		
		List<NameValuePair> paramsMap = new ArrayList<NameValuePair>();
		paramsMap.add(new BasicNameValuePair("access_token", accessTokenManager.getAccessToken()));
		paramsMap.add(new BasicNameValuePair("userid", userId));
	
		return QyHttpClientUtils.httpsGet(QyWechatUrls.DELETE_MEMBER_URL, paramsMap);
	}

	@Override
	public QyMemberResult getMember(String userId) {
		return new Gson().fromJson(getMemberInRaw(userId), QyMemberResult.class);
	}

	@Override
	public String getMemberInRaw(String userId) {
		logger.debug("getMemberInRaw--> User Id:"+userId);
		
		List<NameValuePair> paramsMap = new ArrayList<NameValuePair>();
		paramsMap.add(new BasicNameValuePair("access_token", accessTokenManager.getAccessToken()));
		paramsMap.add(new BasicNameValuePair("userid", userId));
	
		return QyHttpClientUtils.httpsGet(QyWechatUrls.GET_MEMBER_INFO_URL, paramsMap);
	}

	@Override
	public BaseResult batchDeleteMember(QyMemberBatchDeleteReq deleteMembersReq) {
		return new Gson().fromJson(batchDeleteMemberInRaw(deleteMembersReq), BaseResult.class);
	}

	@Override
	public String batchDeleteMemberInRaw(QyMemberBatchDeleteReq deleteMembersReq) {
		logger.debug("batchDeleteMemeberInRaw--> QyMemberBatchDeleteReq :"+deleteMembersReq);
		
		List<NameValuePair> paramsMap = new ArrayList<NameValuePair>();
		paramsMap.add(new BasicNameValuePair("access_token", accessTokenManager.getAccessToken()));

		return QyHttpClientUtils.httpsPostJson(QyWechatUrls.BATCH_DELETE_MEMBER_URL, paramsMap, new Gson().toJson(deleteMembersReq));
	}

	@Override
	public QyMemberListResult listMemberByDept(Integer deptId) {
		return new Gson().fromJson(listMemberByDeptInRaw(deptId,QyFetchChildFlag.UN_FETCH_CHILD,QyMemberStatus.UN_SUBSCRIBE), QyMemberListResult.class);
	}

	@Override
	public QyMemberListResult listMemberByDept(Integer deptId, String fetch_child, Integer status) {
		return new Gson().fromJson(listMemberByDeptInRaw(deptId,fetch_child,status), QyMemberListResult.class);
	}

	@Override
	public String listMemberByDeptInRaw(Integer deptId, String fetch_child, Integer status) {
		logger.debug("listMemberByDeptInRaw: Department Id ->" + deptId + " fetch_child->" + fetch_child + "  status->" + status);
		
		List<NameValuePair> paramsMap = new ArrayList<NameValuePair>();
		paramsMap.add(new BasicNameValuePair("access_token", accessTokenManager.getAccessToken()));
		paramsMap.add(new BasicNameValuePair("department_id", String.valueOf(deptId)));
		paramsMap.add(new BasicNameValuePair("fetch_child", fetch_child));
		paramsMap.add(new BasicNameValuePair("status", String.valueOf(status)));
	
		return QyHttpClientUtils.httpsGet(QyWechatUrls.SIMPLE_LIST_MEMBER_BY_DEPT_URL, paramsMap);
	}

	@Override
	public String listMemberDetailsByDeptInRaw(Integer deptId, String fetch_child, Integer status) {
		logger.debug("listMemberDetailsByDeptInRaw: Department Id ->" + deptId + " fetch_child->" + fetch_child + "  status->" + status);
		
		List<NameValuePair> paramsMap = new ArrayList<NameValuePair>();
		paramsMap.add(new BasicNameValuePair("access_token", accessTokenManager.getAccessToken()));
		paramsMap.add(new BasicNameValuePair("department_id", String.valueOf(deptId)));
		paramsMap.add(new BasicNameValuePair("fetch_child", fetch_child));
		paramsMap.add(new BasicNameValuePair("status", String.valueOf(status)));
	
		return QyHttpClientUtils.httpsGet(QyWechatUrls.DETAIL_LIST_MEMBER_BY_DEPT_URL, paramsMap);
	}

	@Override
	public QyMemberListResult listMemberDetailsByDept(Integer deptId, String fetch_child, Integer status) {
		return new Gson().fromJson(listMemberDetailsByDeptInRaw(deptId,fetch_child, status),QyMemberListResult.class);
	}
	
	@Override
	public QyMemberListResult listMemberDetailsByDept(Integer deptId) {
		return new Gson().fromJson(listMemberDetailsByDeptInRaw(deptId), QyMemberListResult.class);
	}
	
	@Override
	public String listMemberDetailsByDeptInRaw(Integer deptId) {
		return listMemberDetailsByDeptInRaw(deptId,QyFetchChildFlag.UN_FETCH_CHILD,QyMemberStatus.UN_SUBSCRIBE);
	}
	
	
	//-----------------------Getter and Setter----------------------------------------------
	public QyAccessTokenService getAccessTokenManager() {
		return accessTokenManager;
	}

	public void setAccessTokenManager(QyAccessTokenService accessTokenManager) {
		this.accessTokenManager = accessTokenManager;
	}

	@Override
	public QyAccessTokenService getQyAccessTokenService() {
		return accessTokenManager;
	}

	@Override
	public void setQyAccessTokenService(QyAccessTokenService tokenService) {
		this.accessTokenManager = tokenService;
	}
	
}
